package com.starlight.flower_server.infrastructure.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starlight.flower_server.infrastructure.entity.LoginInfo;
import com.starlight.flower_server.infrastructure.mapper.LoginInfoMapper;
import com.starlight.flower_server.infrastructure.repository.LoginInfoRepository;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2023/4/19 8:57
 */
@Repository
public class LoginInfoRepositoryImpl implements LoginInfoRepository {

    @Resource
    private LoginInfoMapper loginInfoMapper;

    @Override
    public LoginInfo queryDetail(Long id) {
        return loginInfoMapper.selectById(id);
    }

    @Override
    public Integer insert(LoginInfo loginInfo) {
        loginInfo.setCreator("SYSTEM");
        loginInfo.setModifier("SYSTEM");
        loginInfo.setCreated(new Date());
        loginInfo.setModified(new Date());
        loginInfo.setIsDeleted(0);
        return loginInfoMapper.insert(loginInfo);
    }

    @Override
    public List<LoginInfo> queryListByFlowerUserIdLatest10(Long flowerUserId) {
        QueryWrapper<LoginInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("created");
        queryWrapper.lambda()
                .eq(LoginInfo::getFlowerUserId,flowerUserId)
                .last("limit 10");
        return loginInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<LoginInfo> queryAllListByFlowerUserId(Long flowerUserId) {
        QueryWrapper<LoginInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("created");
        queryWrapper.lambda()
                .eq(LoginInfo::getFlowerUserId,flowerUserId);
        return loginInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<LoginInfo> queryAll() {
        return loginInfoMapper.selectList(new LambdaQueryWrapper<LoginInfo>()
                .isNotNull(LoginInfo::getId));
    }

    @Override
    public Long queryTotalCount() {
        return loginInfoMapper.selectCount(new LambdaQueryWrapper<LoginInfo>()
                .isNotNull(LoginInfo::getId));
    }

    @Override
    public Integer removeAllByFlowerUserId(Long flowerUserId) {
        return loginInfoMapper.delete(new LambdaQueryWrapper<LoginInfo>()
                .eq(LoginInfo::getFlowerUserId,flowerUserId));
    }
}
